﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Client
{
    public enum ClickSound
    {
        None,
        Stone,
        Glass,
        Norm
    }

    public enum DialogResult : byte
    {
        None = 0,
        Yes = 1,
        No = 2,
        Ok = 3,
        Cancel = 4
    }

    public delegate void OnDirectPaintHandler(object sender, Sprite spriteBatch);
    public delegate void OnKeyPressHandler(object sender, Keys Key);
    public delegate void OnKeyDownHandler(object sender, Keys Key);
    public delegate void OnMouseMoveHandler(object sender, Point ptMouse);
    public delegate void OnMouseDownHandler(object sender, Point ptMouse);
    public delegate void OnMouseUpHandler(object sender, Point ptMouse);
    public delegate void OnDblClickHandler(object sender);
    public delegate void OnClickHandler(object sender);
    public delegate void OnClickExHandler(object sender, Point ptMouse);
    public delegate void OnInRealAreaHandler(object sender, Point ptArea, ref bool IsRealArea);
    public delegate void OnGridSelectHandler(object sender, int ACol, int ARow);
    //TOnGridSelect = procedure(Sender: TObject; ACol, ARow: integer; Shift: TShiftState) of object;
    //TOnGridPaint = procedure(Sender: TObject; ACol, ARow: integer; Rect: TRect; State: TGridDrawState; dsurface: TDirectDrawSurface) of object;
    public delegate void OnClickSoundHandler(object sender, ClickSound ckSound);


    #region WinCtrl
    public class WinCtrl
    {
        public const int WinLeft = 0;
        public const int WinTop = 60;
        public const int WinRight = ClientGlobals.ScreenWidth;
        public const int WinBottom = ClientGlobals.ScreenHeight - 30;


        public static WinCtrl MouseCaptureControl;      //mouse message
        public static WinCtrl FocusedControl;           //Key message
        public static IntPtr MainWinHandle;
        public static WinCtrl ModalDWindow;



        public static void SetFocus(WinCtrl dcon)
        {
            FocusedControl = dcon;
        }

        public static void ReleaseFocus()
        {
            FocusedControl = null;
        }

        public static void SetCapture(WinCtrl dcon)
        {
            //SetCapture(MainWinHandle);
            MouseCaptureControl = dcon;
        }

        public static void ReleaseCapture()
        {
            //ReleaseCapture;
            MouseCaptureControl = null;
        }


        /*public virtual bool OnDirectPaint(SpriteBatch spriteBatch) { return false; }

        public virtual void OnKeyPress(Keys Key) { }
        public virtual void OnKeyDown(Keys Key) { }

        public virtual void OnMouseMove(Point ptMouse) { }
        public virtual void OnMouseDown(Point ptMouse) { }
        public virtual void OnMouseUp(Point ptMouse) { }


        public virtual void OnDblClick() { }
        public virtual void OnClick(Point ptMouse) { }

        public virtual bool OnInRealArea(Point ptArea, ref bool IsRealArea) { return false; }*/


        public event OnDirectPaintHandler OnDirectPaint;

        public event OnKeyPressHandler OnKeyPress;
        public event OnKeyDownHandler OnKeyDown;

        public event OnMouseMoveHandler OnMouseMove;
        public event OnMouseDownHandler OnMouseDown;
        public event OnMouseUpHandler OnMouseUp;

        public event OnDblClickHandler OnDblClick;
        public event OnClickExHandler OnClick;

        public event OnInRealAreaHandler OnInRealArea;
        public event OnClickHandler OnBackgroundClick;


        public int Left;
        public int Top;
        public int Width;
        public int Height;

        protected bool ControlVisible;
        public bool Background;
        public List<WinCtrl> Controls;
        public ImageArray WLib;
        public int FaceIndex;
        public bool WantReturn; //BackgroundÀÏ¶§, ClickÀÇ »ç¿ë ¿©ºÎ..
        public Color DrawColor;

        #region Properties
        public string Caption { get; set; }
        public string Name { get; set; }
        public WinCtrl DParent { get; set; }
        public virtual bool Visible { get { return ControlVisible; } set { ControlVisible = value; } }
        public bool EnableFocus { get; set; }
        #endregion


        public WinCtrl(WinCtrl parentCtrl)
            : base()
        {
            OnDirectPaint = null;
            OnKeyPress = null;
            OnKeyDown = null;
            OnMouseMove = null;
            OnMouseDown = null;
            OnMouseUp = null;
            OnInRealArea = null;

            Controls = new List<WinCtrl>();

            DParent = parentCtrl;
            EnableFocus = false;
            Background = false;

            Width = 80;
            Height = 24;
            Caption = String.Empty;
            Name = String.Empty;
            Visible = true;
            WLib = null;
            WantReturn = false;
            FaceIndex = 0;

            DrawColor = Color.White;

            Loaded();
        }

        public void Loaded()
        {
            if (DParent != null)
            {
                bool bOk = true;

                foreach (WinCtrl con in DParent.Controls)
                {
                    if (con == this)
                    {
                        bOk = false;
                        break;
                    }
                }

                if (bOk)
                    DParent.AddChild(this);
            }
        }

        public int SurfaceX(int x)
        {
            WinCtrl d = this;

            while (true)
            {
                if (d.DParent == null)
                    break;
                x += d.DParent.Left;
                d = d.DParent;
            }
            return x;
        }

        public int SurfaceY(int y)
        {
            WinCtrl d = this;

            while (true)
            {
                if (d.DParent == null)
                    break;
                y += d.DParent.Top;
                d = d.DParent;
            }
            return y;
        }

        public int LocalX(int x)
        {
            WinCtrl d = this;

            while (true)
            {
                if (d.DParent == null)
                    break;
                x -= d.DParent.Left;
                d = d.DParent;
            }
            return x;
        }

        public int LocalY(int y)
        {
            WinCtrl d = this;

            while (true)
            {
                if (d.DParent == null)
                    break;
                y -= d.DParent.Top;
                d = d.DParent;
            }
            return y;
        }

        public void AddChild(WinCtrl dcon)
        {
            Controls.Add(dcon);
        }

        public void ChangeChildOrder(WinCtrl dcon)
        {
            if (!(dcon is Window))
                return;

            if (((Window)dcon).Floating)
            {
                Controls.Remove(dcon);
                Controls.Add(dcon);
            }
        }

        public bool InRange(Point ptMouse)
        {
            bool bRet = false;

            if ((ptMouse.X >= Left) && (ptMouse.X < Left + Width) && (ptMouse.Y >= Top) && (ptMouse.Y < Top + Height))
            {
                bRet = true;
                if (OnInRealArea != null)
                {
                    Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
                    OnInRealArea(this, ptMove, ref bRet);
                }
                else if (WLib != null)
                {
                    Texture d = WLib[FaceIndex];

                    if (d != null)
                    {
                        /*Rectangle srcRect = new Rectangle(x - Left, y - Top, 1, 1);

                        uint[] retrievedColor = new uint[1];

                        d.GetData<uint>(
                                0,
                                srcRect,
                                retrievedColor,
                                0,
                                1);

                        if (retrievedColor[0] <= 0)
                            bRet = false;*/
                    }
                }
            }

            return bRet;
        }

        public virtual bool KeyPress(Keys Key)
        {
            if (Background) return false;

            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].KeyPress(Key))
                        return true;
            }

            if (FocusedControl == this)
            {
                if (OnKeyPress != null)
                    OnKeyPress(this, Key);
                return true;
            }
            return false;
        }

        public virtual bool KeyDown(Keys Key)
        {
            if (Background) return false;

            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].KeyDown(Key))
                        return true;
            }

            if (FocusedControl == this)
            {
                if (OnKeyDown != null)
                    OnKeyDown(this, Key);
                return true;
            }
            return false;
        }

        public bool CanFocusMsg()
        {
            if ((MouseCaptureControl == null) || ((MouseCaptureControl != null) &&
              ((MouseCaptureControl == this) || (MouseCaptureControl == DParent))))
            {
                return true;
            }
            return false;
        }

        public virtual bool MouseMove(Point ptMouse)
        {
            Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].MouseMove(ptMove))
                        return true;
            }

            if (MouseCaptureControl != null)
            {
                if (MouseCaptureControl == this)
                {
                    if (OnMouseMove != null)
                        OnMouseMove(this, ptMouse);
                    return true;
                }
                return false;
            }

            if (Background) return false;

            if (InRange(ptMouse))
            {
                if (OnMouseMove != null)
                    OnMouseMove(this, ptMouse);
                return true;
            }
            return false;
        }

        public virtual bool MouseDown(Point ptMouse)
        {
            Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].MouseDown(ptMove))
                        return true;
            }

            if (Background)
            {
                if (OnBackgroundClick != null)
                {
                    WantReturn = false;
                    OnBackgroundClick(this);
                    if (WantReturn)
                        return true;
                }
                ReleaseFocus();
                return false;
            }

            if (CanFocusMsg())
            {
                if (InRange(ptMouse) || (MouseCaptureControl == this))
                {
                    if (OnMouseDown != null)
                        OnMouseDown(this, ptMouse);
                    if (EnableFocus)
                        SetFocus(this);
                    //else ReleaseFocus();
                    return true;
                }
            }
            return false;
        }

        public virtual bool MouseUp(Point ptMouse)
        {
            Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].MouseUp(ptMove))
                        return true;
            }

            if (MouseCaptureControl != null)   //MouseCapture ÀÌ¸é ÀÚ½ÅÀÌ ¿ì¼±
            {
                if (MouseCaptureControl == this)
                {
                    if (OnMouseUp != null)
                        OnMouseUp(this, ptMouse);
                    return true;
                }
                return false;
            }

            if (Background) return false;

            if (InRange(ptMouse))
            {
                if (OnMouseUp != null)
                    OnMouseUp(this, ptMouse);
                return true;
            }
            return false;
        }

        public virtual bool DblClick(Point ptMouse)
        {
            if (MouseCaptureControl != null) //MouseCapture ÀÌ¸é ÀÚ½ÅÀÌ ¿ì¼±
            {
                if (MouseCaptureControl == this)
                {
                    if (OnDblClick != null)
                        OnDblClick(this);
                    return true;
                }
                return false;
            }

            Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].DblClick(ptMove))
                        return true;
            }

            if (Background) return false;

            if (InRange(ptMouse))
            {
                if (OnDblClick != null)
                    OnDblClick(this);
                return true;
            }
            return false;
        }

        public virtual bool Click(Point ptMouse)
        {
            if (MouseCaptureControl != null)   //MouseCapture ÀÌ¸é ÀÚ½ÅÀÌ ¿ì¼±
            {
                if (MouseCaptureControl == this)
                {
                    if (OnClick != null)
                        OnClick(this, ptMouse);
                    return true;
                }
                return false;
            }

            Point ptMove = new Point(ptMouse.X - Left, ptMouse.Y - Top);
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                if (Controls[i].Visible)
                    if (Controls[i].Click(ptMove))
                        return true;
            }

            if (Background) return false;

            if (InRange(ptMouse))
            {
                if (OnClick != null)
                    OnClick(this, ptMouse);
                return true;
            }

            return false;
        }

        public void SetImgIndex(ImageArray Lib, int index)
        {
            //FaceSurface := dsurface;
            if (Lib != null)
            {
                Texture d = Lib[index];
                WLib = Lib;
                FaceIndex = index;
                if (d != null)
                {
                    Width = d.GetLevelDescription(0).Width;
                    Height = d.GetLevelDescription(0).Height;
                }
            }
        }

        public void DirectPaint(Sprite spriteBatch)
        {
            if (OnDirectPaint != null)
                OnDirectPaint(this, spriteBatch);
            else if (WLib != null)
            {
                Texture d = WLib[FaceIndex];
                if (d != null)
                    spriteBatch.Draw(d, Rectangle.Empty, Vector3.Empty, new Vector3(SurfaceX(Left), SurfaceY(Top), 0), DrawColor);
            }

            for (int i = 0; i < Controls.Count; i++)
            {
                if (Controls[i].Visible)
                    Controls[i].DirectPaint(spriteBatch);
            }
        }
    } 
    #endregion

    #region Button
    public class Button : WinCtrl
    {
        public ClickSound ClickSound
        {
            get;
            set;
        }

        public bool Downed;
        public Color TextColor;
        public Color BackColor;

        public new event OnClickExHandler OnClick;
        public event OnClickSoundHandler OnClickSound;


        public Button(WinCtrl parentCtrl)
            : base(parentCtrl)
        {
            Downed = false;
            TextColor = Color.Black;
            BackColor = Color.White;
            OnClick = null;
            OnClickSound = null;
            EnableFocus = true;
            ClickSound = ClickSound.None;
        }

        public override bool MouseMove(Point ptMouse)
        {
            bool bRet = base.MouseMove(ptMouse);
            if (!Background && !bRet)
            {
                bRet = base.MouseMove(ptMouse);
                if (MouseCaptureControl == this)
                {
                    if (InRange(ptMouse)) Downed = true; else Downed = false;
                }
            }
            return bRet;
        }

        public override bool MouseDown(Point ptMouse)
        {
            bool bRet = false;
            if (base.MouseDown(ptMouse))
            {
                if (!Background && MouseCaptureControl == null)
                {
                    Downed = true;
                    SetCapture(this);
                }
                bRet = true;
            }
            return bRet;
        }

        public override bool MouseUp(Point ptMouse)
        {
            bool bRet = false;
            if (base.MouseUp(ptMouse))
            {
                ReleaseCapture();
                if (!Background)
                {
                    if (InRange(ptMouse))
                    {
                        if (OnClickSound != null)
                            OnClickSound(this, ClickSound);
                        if (OnClick != null)
                            OnClick(this, ptMouse);
                    }
                }
                Downed = false;
                bRet = true;
                return bRet;
            }
            else
            {
                ReleaseCapture();
                Downed = false;
            }
            return bRet;
        }
    } 
    #endregion

    #region Window
    public class Window : Button
    {
        public bool Floating
        {
            get;
            set;
        }

        public override bool Visible
        {
            get { return ControlVisible; }
            set
            {
                ControlVisible = value;
                if (Floating)
                    if (DParent != null)
                        DParent.ChangeChildOrder(this);
            }
        }

        public Point Spot;
        public DialogResult DialogResult;

        public Window(WinCtrl parentCtrl)
            : base(parentCtrl)
        {
            Floating = false;
            EnableFocus = true;
            Spot = Point.Empty;

            Width = 120;
            Height = 120;
        }

        public override bool MouseMove(Point ptMouse)
        {
            bool bRet = base.MouseMove(ptMouse);
            if (bRet && Floating && MouseCaptureControl == this)
            {
                if (Spot != ptMouse)
                {
                    int al = Left + (ptMouse.X - Spot.X);
                    int at = Top + (ptMouse.Y - Spot.Y);
                    if (al + Width < WinLeft)
                        al = WinLeft - Width;
                    if (al > WinRight)
                        al = WinRight;
                    if (at + Height < WinTop)
                        at = WinTop - Height;
                    if (at + Height > WinBottom)
                        at = WinBottom - Height;
                    Left = al;
                    Top = at;
                    Spot = ptMouse;
                }
            }
            return bRet;
        }

        public override bool MouseDown(Point ptMouse)
        {
            bool bRet = base.MouseDown(ptMouse);
            if (bRet)
            {
                if (Floating)
                {
                    if (DParent != null)
                        DParent.ChangeChildOrder(this);
                }
                Spot = ptMouse;
            }
            return bRet;
        }

        public override bool MouseUp(Point ptMouse)
        {
            return base.MouseUp(ptMouse);
        }


        public void Show()
        {
            Visible = true;
            if (Floating)
            {
                if (DParent != null)
                    DParent.ChangeChildOrder(this);
            }
            if (EnableFocus) SetFocus(this);
        }

        public int ShowModal()
        {
            int nRet = 0;

            Visible = true;
            ModalDWindow = this;
            if (EnableFocus)
            {
                SetFocus(this);
                nRet = 1;
            }

            nRet = 2;

            return nRet;
        }
    } 
    #endregion


    public sealed class WinManager
    {
        public static readonly WinManager Me = new WinManager();

        public List<WinCtrl> WinList;
        public List<WinCtrl> WinListTemp;

        private WinManager()
            : base()
        {
            WinList = new List<WinCtrl>();
            WinListTemp = new List<WinCtrl>();

            WinCtrl.MouseCaptureControl = null;
            WinCtrl.FocusedControl = null;
        }


        public void AddControl(WinCtrl con, bool visible)
        {
            con.Visible = visible;
            WinList.Add(con);
        }

        public void DelControl(WinCtrl con)
        {
            for (int i = 0; i < WinList.Count; i++)
            {
                if (WinList[i] == con)
                {
                    WinList.RemoveAt(i);
                    break;
                }
            }
        }

        public WinCtrl this[int nIndex]
        {
            get { return WinList[nIndex]; }
        }

        public void ClearAll()
        {
            WinList.Clear();
        }

        public void HideAllControls()
        {
            WinListTemp.Clear();

            for (int i = 0; i < WinList.Count; i++)
            {
                WinCtrl c = WinList[i];
                if (c.Visible)
                {
                    WinListTemp.Add(c);
                    c.Visible = false;
                }
            }
        }

        public void RestoreHideControls()
        {
            for (int i = 0; i < WinListTemp.Count; i++)
                WinListTemp[i].Visible = true;

        }

        public bool KeyPress(ref Keys Key)
        {
            bool bRet = false;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                    return WinCtrl.ModalDWindow.KeyPress(Key);
                else
                    WinCtrl.ModalDWindow = null;

                Key = Keys.None; //ModalDWindow°¡ KeyDownÀ» °ÅÄ¡¸é¼­ Visible=false·Î º¯ÇÏ¸é¼­
                //KeyPress¸¦ ´Ù½Ã°ÅÃÄ¼­ ModalDwindow=nilÀÌ µÈ´Ù.
            }

            if (WinCtrl.FocusedControl != null)
            {
                if (WinCtrl.FocusedControl.Visible)
                    bRet = WinCtrl.FocusedControl.KeyPress(Key);
                else
                    WinCtrl.ReleaseFocus();
            }

            /*for i:=0 to DWinList.Count-1 do begin
               if TDControl(DWinList[i]).Visible then begin
                  if TDControl(DWinList[i]).KeyPress (Key) then begin
                     Result := TRUE;
                     break;
                  end;
               end;
            end;*/
            return bRet;
        }

        public bool KeyDown(Keys Key)
        {
            bool bRet = false;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                    return WinCtrl.ModalDWindow.KeyDown(Key);
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.FocusedControl != null)
            {
                if (WinCtrl.FocusedControl.Visible)
                    bRet = WinCtrl.FocusedControl.KeyDown(Key);
                else
                    WinCtrl.ReleaseFocus();
            }

            /*for i:=0 to DWinList.Count-1 do begin
               if TDControl(DWinList[i]).Visible then begin
                  if TDControl(DWinList[i]).KeyDown (Key, Shift) then begin
                     Result := TRUE;
                     break;
                  end;
               end;
            end;*/
            return bRet;
        }

        public bool MouseMove(Point ptMouse)
        {
            bool bRet = false;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                {
                    Point ptMove = new Point(WinCtrl.ModalDWindow.LocalX(ptMouse.X),
                        WinCtrl.ModalDWindow.LocalY(ptMouse.Y));
                    return WinCtrl.ModalDWindow.MouseMove(ptMove);
                    //return true;
                }
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.MouseCaptureControl != null)
            {
                Point ptMove = new Point(WinCtrl.MouseCaptureControl.LocalX(ptMouse.X),
                    WinCtrl.MouseCaptureControl.LocalY(ptMouse.Y));
                bRet = WinCtrl.MouseCaptureControl.MouseMove(ptMove);
            }
            else
            {
                for (int i = 0; i < WinList.Count; i++)
                {
                    if (WinList[i].Visible)
                        if (WinList[i].MouseMove(ptMouse))
                            return true;
                }
            }
            return bRet;
        }

        public bool MouseDown(Point ptMouse)
        {
            bool bRet = false;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                {
                    Point ptMove = new Point(WinCtrl.ModalDWindow.LocalX(ptMouse.X),
                                    WinCtrl.ModalDWindow.LocalY(ptMouse.Y));
                    return WinCtrl.ModalDWindow.MouseDown(ptMove);
                    //return true;
                }
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.MouseCaptureControl != null)
            {
                Point ptMove = new Point(WinCtrl.MouseCaptureControl.LocalX(ptMouse.X),
                              WinCtrl.MouseCaptureControl.LocalY(ptMouse.Y));
                bRet = WinCtrl.MouseCaptureControl.MouseDown(ptMove);
            }
            else
            {
                for (int i = 0; i < WinList.Count; i++)
                {
                    if (WinList[i].Visible)
                        if (WinList[i].MouseDown(ptMouse))
                            return true;
                }
            }
            return bRet;
        }

        public bool MouseUp(Point ptMouse)
        {
            bool bRet = true;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                {
                    Point ptMove = new Point(WinCtrl.ModalDWindow.LocalX(ptMouse.X),
                                    WinCtrl.ModalDWindow.LocalY(ptMouse.Y));
                    return WinCtrl.ModalDWindow.MouseUp(ptMove);
                }
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.MouseCaptureControl != null)
            {
                Point ptMove = new Point(WinCtrl.MouseCaptureControl.LocalX(ptMouse.X),
                              WinCtrl.MouseCaptureControl.LocalY(ptMouse.Y));
                bRet = WinCtrl.MouseCaptureControl.MouseUp(ptMove);
            }
            else
            {
                for (int i = 0; i < WinList.Count; i++)
                {
                    if (WinList[i].Visible)
                        if (WinList[i].MouseUp(ptMouse))
                            return true;
                }
            }
            return bRet;
        }

        public bool DblClick(Point ptMouse)
        {
            bool bRet = true;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                {
                    Point ptMove = new Point(WinCtrl.ModalDWindow.LocalX(ptMouse.X),
                                    WinCtrl.ModalDWindow.LocalY(ptMouse.Y));
                    return WinCtrl.ModalDWindow.DblClick(ptMove);
                }
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.MouseCaptureControl != null)
            {
                Point ptMove = new Point(WinCtrl.MouseCaptureControl.LocalX(ptMouse.X),
                              WinCtrl.MouseCaptureControl.LocalY(ptMouse.Y));
                bRet = WinCtrl.MouseCaptureControl.DblClick(ptMove);
            }
            else
            {
                for (int i = 0; i < WinList.Count; i++)
                {
                    if (WinList[i].Visible)
                        if (WinList[i].DblClick(ptMouse))
                            return true;
                }
            }
            return bRet;
        }

        public bool Click(Point ptMouse)
        {
            bool bRet = true;

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                {
                    Point ptMove = new Point(WinCtrl.ModalDWindow.LocalX(ptMouse.X),
                                    WinCtrl.ModalDWindow.LocalY(ptMouse.Y));
                    return WinCtrl.ModalDWindow.Click(ptMove);
                }
                else
                    WinCtrl.ModalDWindow = null;
            }

            if (WinCtrl.MouseCaptureControl != null)
            {
                Point ptMove = new Point(WinCtrl.MouseCaptureControl.LocalX(ptMouse.X),
                              WinCtrl.MouseCaptureControl.LocalY(ptMouse.Y));
                bRet = WinCtrl.MouseCaptureControl.Click(ptMove);
            }
            else
            {
                for (int i = 0; i < WinList.Count; i++)
                {
                    if (WinList[i].Visible)
                        if (WinList[i].Click(ptMouse))
                            return true;
                }
            }
            return bRet;
        }

        public void DirectPaint(Sprite sb)
        {
            sb.Begin(SpriteFlags.AlphaBlend);
            for (int i = 0; i < WinList.Count; i++)
            {
                if (WinList[i].Visible)
                {
                    WinList[i].DirectPaint(sb);
                }
            }

            if (WinCtrl.ModalDWindow != null)
            {
                if (WinCtrl.ModalDWindow.Visible)
                    WinCtrl.ModalDWindow.DirectPaint(sb);
            }
            sb.End();
        }
    }

}